package com.schoolCommunity.service.impl;

import com.schoolCommunity.entity.ForIndex;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.mapper.ForIndexMapper;
import com.schoolCommunity.service.ForIndexService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolCommunity.util.InsertES;
import com.schoolCommunity.util.RedisUtil;
import com.schoolCommunity.util.UploadPlacePicture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-10-08
 */
@Service
public class ForIndexServiceImpl extends ServiceImpl<ForIndexMapper, ForIndex> implements ForIndexService {

    @Resource
    private ForIndexMapper forIndexMapper;

    @Autowired
    @Qualifier("MyTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private InsertES insertES;
    @Autowired
    private UploadPlacePicture uploadPlacePicture;

    //添加介绍
    @Override
    public Information InsertForIndex(ForIndex forIndex) throws IOException {
        Information information = new Information();

        int insert = forIndexMapper.insert(forIndex);
//        获取插入的id
        long lastId = forIndexMapper.getLastId();
        //获取数据库的一些默认值
//        获取不需要？
        forIndex = forIndexMapper.selectById(lastId);
        //放入ES
        insertES.InsertForIndex(forIndex);


        information.setMessage("有" + insert + "条数据插入成功").setCode(200);
        return information;
    }

    //添加或者修改图片
    @Override
    public Information UploadPlacePicture(MultipartFile file, long forIndexId) throws IOException {
        Information information = new Information();
        ForIndex forIndex = forIndexMapper.selectById(forIndexId);

        String pictureURL = uploadPlacePicture.upload(file, forIndex.getPlaceName()); //写入了图片
        //删除原来的图片

        if (!forIndex.getPictureUrl().equals("default.jpg")) {
            File f2 = new File("E:/项目/学校社区/schoolCommunityVue/src/schoolCommunityPicture/placePicture/"+forIndex.getPictureUrl());
            //先删除原来的图片
            f2.delete();

            File f3 = new File("E:/项目/学校社区/vue-admin-template/src/views/schoolCommunityPicture/placePicture/"+forIndex.getPictureUrl());
            //先删除原来的图片
            f3.delete();
        }


        forIndex.setPictureUrl(pictureURL);
        int i = forIndexMapper.updateById(forIndex);
        if (i > 0) {
            insertES.UpdateForIndexPicture(pictureURL, forIndexId);
            information.setCode(200).setMessage("图片写入成功");
        } else {
            information.setCode(404).setMessage("图片写入失败");
        }
        return information;
    }

    //删除对象
    @Override
    public Information DeleteForIndex(long id) throws IOException {
        Information information = new Information();
        int i = forIndexMapper.deleteById(id);
        if (i > 0) {
            insertES.DeleteForIndex(id);
            information.setCode(200).setMessage("介绍删除成功");
        } else {
            information.setCode(404).setMessage("介绍删除失败");
        }
        return information;
    }

    //修改对象
    @Override
    public Information UpdateForIndex(ForIndex forIndex) throws IOException {
        Information information = new Information();

        int i = forIndexMapper.updateById(forIndex);
        if (i > 0) {
            insertES.UpdateForIndex(forIndex);
            information.setCode(200).setMessage("介绍修改成功");
        } else {
            information.setCode(404).setMessage("介绍修改失败");
        }
        return information;

    }

    //获取所有介绍
    @Override
    public Information GetAllForIndex() {
        Information information = new Information();
        List<ForIndex> forIndices = forIndexMapper.selectList(null);
        information.setCode(200).setMessage("获取所有数据成功，共" + forIndices.size() + "条介绍").setData(forIndices);
        return information;
    }

    //点赞介绍
    @Override
    public Information AddLovesForIndex(long id, long memberId) {
        Information information = new Information();
        //先判断是否存在redis
        String redisKey = "AddLovesForIndex:" + Integer.toString((int) id) + ":" + Integer.toString((int) memberId);
        if (redisUtil.CheckRedis(redisKey)) {
            //存在key
            information.setMessage("一天只能点赞一次哦").setCode(404);
        } else {
            //不存在key
            //执行点赞
            Integer integer = forIndexMapper.AddLovesForIndex(id);
            if (integer > 0) {
                //数据库已经点赞成功
                //redis记载
                redisUtil.InsertRedis(redisKey, redisKey);
                information.setMessage("点赞成功").setCode(200);
            } else {
                information.setMessage("点赞失败").setCode(404);
            }
        }


        return information;
    }


}
